package hivi.vn.hivigold.fragment;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.activities.SelectEmployee;
import hivi.vn.hivigold.activities.SelectPartner;
import hivi.vn.hivigold.activities.SelectStock;
import hivi.vn.hivigold.adapters.BuyAdapter;
import hivi.vn.hivigold.adapters.NXTAdapter;
import hivi.vn.hivigold.adapters.SaleAdapter;
import hivi.vn.hivigold.database.Preferences;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Employee;
import hivi.vn.hivigold.objects.Partner;
import hivi.vn.hivigold.objects.Report;
import hivi.vn.hivigold.objects.ReportFilter;
import hivi.vn.hivigold.objects.Stock;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ReportFragment extends Fragment {
	int CURRENT_REPORT = -1;
	public static final int REPORT_NXT = 1;
	public static final int REPORT_BAN = 2;
	public static final int REPORT_MUA = 3;
	int flagDate = -1;
	static final int DFROM = 1;
	static final int DTO = 2;

	Button btnDoanhthu;
	Button btnNhapmua;
	Button btnNXT;
	Button btnExport;
	TextView txtDfrom;
	TextView txtDto;
	TextView txtVtID;
	TextView txtNvID;
	TextView txtKhID;
	TextView txtKhoID;
	TextView txtNhID;
	ListView lstReport;

	TextView txtTotal_Sl_dt;
	TextView txtTotal_Tien_ban_dt;
	TextView txtTotal_Tien_von_dt;
	TextView txtTotal_Lai_dt;
	TextView txtTotal_Sl_m;
	TextView txtTotal_Tien_m;
	
	TextView txtTotal_ton_dau;
	TextView txtTotal_au_td;
	TextView txtTotal_du_dau;
	TextView txtTotal_sl_nhap;
	TextView txtTotal_au_nhap;
	TextView txtTotal_tien_nhap;
	TextView txtTotal_sl_xuat;
	TextView txtTotal_au_xuat;
	TextView txtTotal_tien_xuat;
	TextView txtTotal_ton_cuoi;
	TextView txtTotal_au_tc;
	TextView txtTotal_du_cuoi;
	
	LinearLayout lnlBuy;
	LinearLayout lnlSale;
	LinearLayout lnlNXT;
	LinearLayout lnlTotalBuy;
	LinearLayout lnlTotalSale;
	LinearLayout lnlTotalNXT;

	int mYear;
	int mMonth;
	int mDay;
	Date dFrom;
	Date dTo;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.report_layout, null);
		btnDoanhthu = (Button) v.findViewById(R.id.btnReportDT);
		btnNhapmua = (Button) v.findViewById(R.id.btnReportNM);
		btnNXT = (Button) v.findViewById(R.id.btnReportNXT);
		btnExport = (Button) v.findViewById(R.id.btnExport);

		txtDfrom = (EditText) v.findViewById(R.id.txtDFrom);
		txtDto = (EditText) v.findViewById(R.id.txtDTo);
		txtVtID = (EditText) v.findViewById(R.id.txtVtID);
		txtKhID = (EditText) v.findViewById(R.id.txtKhID);
		txtNhID = (EditText) v.findViewById(R.id.txtNhomID);
		txtNvID = (EditText) v.findViewById(R.id.txtNvID);
		txtKhoID = (EditText) v.findViewById(R.id.txtKhoID);
		lstReport = (ListView) v.findViewById(R.id.lstReport);
		lnlBuy = (LinearLayout) v.findViewById(R.id.lnlDoanhthu);
		lnlSale = (LinearLayout) v.findViewById(R.id.lnlNhapmua);
		lnlNXT = (LinearLayout) v.findViewById(R.id.lnlTonkho);
		lnlTotalBuy = (LinearLayout) v.findViewById(R.id.lnlTotalDoanhthu);
		lnlTotalSale = (LinearLayout) v.findViewById(R.id.lnlTotalNhapmua);
		lnlTotalNXT = (LinearLayout) v.findViewById(R.id.lnlTotalNhapxuatton);
		
		txtTotal_Sl_dt = (TextView) v.findViewById(R.id.tvTotal_sl_dt);
		txtTotal_Tien_ban_dt = (TextView) v.findViewById(R.id.tv_total_tien_ban);
		txtTotal_Tien_von_dt = (TextView) v.findViewById(R.id.tv_total_tien_von);
		txtTotal_Lai_dt = (TextView) v.findViewById(R.id.tv_total_lai_ban);
		
		txtTotal_Sl_m = (TextView) v.findViewById(R.id.tvTotal_sl_m);
		txtTotal_Tien_m = (TextView) v.findViewById(R.id.tv_total_tien_mua);
		
		txtTotal_du_dau = (TextView) v.findViewById(R.id.tvd_du_dau);
		txtTotal_du_cuoi = (TextView) v.findViewById(R.id.tvd_du_cuoi);
		txtTotal_au_td = (TextView) v.findViewById(R.id.tvd_tlgau_td);
		txtTotal_au_tc = (TextView) v.findViewById(R.id.tvd_tlgau_tc);
		txtTotal_ton_dau = (TextView) v.findViewById(R.id.tvd_sl_td);
		txtTotal_ton_cuoi = (TextView) v.findViewById(R.id.tvd_sl_tc);
		txtTotal_tien_nhap = (TextView) v.findViewById(R.id.tvd_tien_nhap);
		txtTotal_tien_xuat = (TextView) v.findViewById(R.id.tvd_tien_xuat);
		txtTotal_au_nhap = (TextView) v.findViewById(R.id.tvd_tlgau_nhap);
		txtTotal_au_xuat = (TextView) v.findViewById(R.id.tvd_tlgau_xuat);
		txtTotal_sl_nhap = (TextView) v.findViewById(R.id.tvd_sl_nhap);
		txtTotal_sl_xuat = (TextView) v.findViewById(R.id.tvd_sl_xuat);
		
		txtKhoID = (EditText) v.findViewById(R.id.txtKhoID);
		lstReport = (ListView) v.findViewById(R.id.lstReport);
		
		btnDoanhthu.setOnClickListener(onClick);
		btnNhapmua.setOnClickListener(onClick);
		btnNXT.setOnClickListener(onClick);
		btnExport.setOnClickListener(onClick);

		dFrom = Formats.CurrentDate();
		dTo = Formats.CurrentDate();
		txtDfrom.setText(Formats.DateFormatDisplay(dFrom));
		txtDto.setText(Formats.DateFormatDisplay(dTo));
		txtKhoID.setText(Preferences.getPreferenceValue(getActivity(), Preferences.STOCKID).trim());

		txtDfrom.setOnClickListener(onClick);
		txtDto.setOnClickListener(onClick);
		txtKhID.setOnLongClickListener(onLong);
		txtKhoID.setOnLongClickListener(onLong);
		txtNvID.setOnLongClickListener(onLong);
		
		setLayout(CURRENT_REPORT);
		return v;
	}

	DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			if (flagDate == DFROM) {
				dFrom = Formats.createDate(year, monthOfYear, dayOfMonth);
				txtDfrom.setText(Formats.DateFormatDisplay(dFrom));
			} else if (flagDate == DTO) {
				dTo = Formats.createDate(year, monthOfYear, dayOfMonth);
				txtDto.setText(Formats.DateFormatDisplay(dTo));
			}
			flagDate = -1;
		}
	};

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0.getId() == R.id.btnReportDT) {
				btnDoanhthu.setBackgroundResource(R.drawable.info_tabb_dgrey);
				btnNhapmua.setBackgroundResource(R.drawable.custom_button);
				btnNXT.setBackgroundResource(R.drawable.custom_button);
				CURRENT_REPORT = REPORT_BAN;
			} else if (arg0.getId() == R.id.btnReportNM) {
				btnDoanhthu.setBackgroundResource(R.drawable.custom_button);
				btnNhapmua.setBackgroundResource(R.drawable.info_tabb_dgrey);
				btnNXT.setBackgroundResource(R.drawable.custom_button);
				CURRENT_REPORT = REPORT_MUA;
			} else if (arg0.getId() == R.id.btnReportNXT) {
				btnDoanhthu.setBackgroundResource(R.drawable.custom_button);
				btnNhapmua.setBackgroundResource(R.drawable.custom_button);
				btnNXT.setBackgroundResource(R.drawable.info_tabb_dgrey);
				CURRENT_REPORT = REPORT_NXT;
			} else if (arg0.getId() == R.id.txtDFrom) {
				flagDate = DFROM;
				DatePickerDialog mDatePickerDialog = new DatePickerDialog(
						getActivity(), mDateSetListener, Formats.getComDateNow(
								dFrom, "yyyy"), (Formats.getComDateNow(dFrom,
								"MM") - 1), Formats.getComDateNow(dFrom, "dd"));
				mDatePickerDialog.setTitle(R.string.dfrom);
				mDatePickerDialog.show();
			} else if (arg0.getId() == R.id.txtDTo) {
				flagDate = DTO;
				DatePickerDialog mDatePickerDialog = new DatePickerDialog(
						getActivity(), mDateSetListener, Formats.getComDateNow(
								dTo, "yyyy"),
						(Formats.getComDateNow(dTo, "MM") - 1),
						Formats.getComDateNow(dTo, "dd"));
				mDatePickerDialog.setTitle(R.string.dto);
				mDatePickerDialog.show();
			} else if (arg0.getId() == R.id.btnExport) {
				if (CURRENT_REPORT == -1) {
					Toast.makeText(getActivity(),
							R.string.warning_choose_report, Toast.LENGTH_LONG)
							.show();
					return;
				}
				if (txtDfrom.getText().toString().trim().length() == 0 ||txtDto.getText().toString().trim().length() == 0 ||txtKhoID.getText().toString().trim().length() == 0 )
				{
					Toast.makeText(getActivity(),
							R.string.warning_notempty, Toast.LENGTH_LONG)
							.show();
					return;
				}
				ReportFilter filter = new ReportFilter();
				filter.setDfrom(dFrom);
				filter.setDto(dTo);
				filter.setEmployee(txtNvID.getText().toString().trim());
				filter.setGroup(txtNhID.getText().toString().trim());
				filter.setItem(txtVtID.getText().toString().trim());
				filter.setPartner(txtKhID.getText().toString().trim());
				filter.setStock(txtKhoID.getText().toString().trim());
				filter.setREPORTID(CURRENT_REPORT);
				ReportTask ban = new ReportTask();
				ban.execute(filter);
			}

		}
	};

	private class ReportTask extends
			AsyncTask<ReportFilter, Void, ArrayList<Report>> {
		ProgressDialog dialog;
		int currentReport;

		@Override
		protected void onPostExecute(ArrayList<Report> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (currentReport == REPORT_BAN) {
				if(result.size()>0)
				{
					Report r = result.get(result.size()-1);
					result.remove(result.size()-1);
					txtTotal_Sl_dt.setText(Formats.number(r.getSl_ban()));
					txtTotal_Tien_ban_dt.setText(Formats.money(r.getTien_ban()/1000));
					txtTotal_Tien_von_dt.setText(Formats.money(r.getTien_von()/1000));
					txtTotal_Lai_dt.setText(Formats.money(r.getLai()/1000));
				}else
				{
					txtTotal_Sl_dt.setText("0");
					txtTotal_Tien_ban_dt.setText("0");
					txtTotal_Tien_von_dt.setText("0");
					txtTotal_Lai_dt.setText("0");
				}
				BuyAdapter adapter = new BuyAdapter(getActivity(), result);
				lstReport.setAdapter(adapter);
			} else if (currentReport == REPORT_MUA) {
				if(result.size()>0)
				{
					Report r = result.get(result.size()-1);
					result.remove(result.size()-1);
					txtTotal_Sl_m.setText(Formats.number(r.getSl_mua()));
					txtTotal_Tien_m.setText(Formats.money(r.getTien_mua()/1000));
				}else
				{
					txtTotal_Sl_m.setText("0");
					txtTotal_Tien_m.setText("0");
				}
				SaleAdapter adapter = new SaleAdapter(getActivity(), result);
				lstReport.setAdapter(adapter);
			} else if (currentReport == REPORT_NXT) {
				if(result.size()>0)
				{
					Report r = result.get(result.size()-1);
					result.remove(result.size()-1);
					txtTotal_ton_dau.setText(Formats.number(r.getSl_td()));
					txtTotal_ton_cuoi.setText(Formats.number(r.getSl_tc()));
					txtTotal_sl_nhap.setText(Formats.number(r.getSl_nhap()));
					txtTotal_sl_xuat.setText(Formats.number(r.getSl_xuat()));
					txtTotal_du_dau.setText(Formats.money(r.getDu_dau()/1000));
					txtTotal_du_cuoi.setText(Formats.money(r.getDu_cuoi()/1000));
					txtTotal_tien_nhap.setText(Formats.money(r.getTien_nhap()/1000));
					txtTotal_tien_xuat.setText(Formats.money(r.getTien_xuat()/1000));
					txtTotal_au_td.setText(Formats.number(r.getAu_td()));
					txtTotal_au_tc.setText(Formats.number(r.getAu_tc()));
					txtTotal_au_nhap.setText(Formats.number(r.getAu_nhap()));
					txtTotal_au_xuat.setText(Formats.number(r.getAu_xuat()));
				}else
				{
					txtTotal_ton_dau.setText("0");
					txtTotal_ton_cuoi.setText("0");
					txtTotal_sl_nhap.setText("0");
					txtTotal_sl_xuat.setText("0");
					txtTotal_du_dau.setText("0");
					txtTotal_du_cuoi.setText("0");
					txtTotal_tien_nhap.setText("0");
					txtTotal_tien_xuat.setText("0");
					txtTotal_au_td.setText("0");
					txtTotal_au_tc.setText("0");
					txtTotal_au_nhap.setText("0");
					txtTotal_au_xuat.setText("0");
				}
				NXTAdapter adapter = new NXTAdapter(getActivity(), result);
				lstReport.setAdapter(adapter);
			}
			setLayout(currentReport);
			dialog.dismiss();
		}
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(getActivity());
			dialog.setMessage(getResources().getString(R.string.alert_waiting));
			dialog.show();
		}

		@Override
		protected ArrayList<Report> doInBackground(ReportFilter... p) {
			// TODO Auto-generated method stub
			currentReport = p[0].getREPORTID();
			return WebService.getReport(getActivity(), p[0]);
		}
	}
	private void setLayout(int key)
	{
		if(key == REPORT_BAN)
		{
			lnlBuy.setVisibility(View.VISIBLE);
			lnlSale.setVisibility(View.GONE);
			lnlNXT.setVisibility(View.GONE);
			lnlTotalBuy.setVisibility(View.VISIBLE);
			lnlTotalSale.setVisibility(View.GONE);
			lnlTotalNXT.setVisibility(View.GONE);
		}else if(key == REPORT_MUA)
		{
			lnlBuy.setVisibility(View.GONE);
			lnlSale.setVisibility(View.VISIBLE);
			lnlNXT.setVisibility(View.GONE);
			lnlTotalBuy.setVisibility(View.GONE);
			lnlTotalSale.setVisibility(View.VISIBLE);
			lnlTotalNXT.setVisibility(View.GONE);
		}else if(key == REPORT_NXT)
		{
			lnlBuy.setVisibility(View.GONE);
			lnlSale.setVisibility(View.GONE);
			lnlNXT.setVisibility(View.VISIBLE);
			lnlTotalBuy.setVisibility(View.GONE);
			lnlTotalSale.setVisibility(View.GONE);
			lnlTotalNXT.setVisibility(View.VISIBLE);
		}else
		{
			lnlBuy.setVisibility(View.GONE);
			lnlSale.setVisibility(View.GONE);
			lnlNXT.setVisibility(View.GONE);
			lnlTotalBuy.setVisibility(View.GONE);
			lnlTotalSale.setVisibility(View.GONE);
			lnlTotalNXT.setVisibility(View.GONE);
		}
	}
	OnLongClickListener onLong = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.txtNvID) {
				Intent i = new Intent(getActivity(), SelectEmployee.class);
				startActivityForResult(i, SelectEmployee.SELECTED_EMPLOYEE_KEY);
			} else if (v.getId() == R.id.txtKhoID) {
				Intent i = new Intent(getActivity(), SelectStock.class);
				startActivityForResult(i, SelectStock.SELECTED_STOCK_KEY);
			} else if (v.getId() == R.id.txtKhID)
			{
				Intent i = new Intent(getActivity(), SelectPartner.class);
				startActivityForResult(i, SelectPartner.SELECTED_PARTNER_KEY);
			}
			return true;
		}
	};
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == getActivity().RESULT_OK) {
			Bundle bundle = data.getExtras();
			switch (requestCode) {
			case SelectPartner.SELECTED_PARTNER_KEY:
				Partner p = (Partner) bundle
						.getSerializable(SelectPartner.SELECTED_PARTNER);
				txtKhID.setText(p.getPartnerID());
				break;
			case SelectEmployee.SELECTED_EMPLOYEE_KEY:
				Employee e = (Employee) bundle
						.getSerializable(SelectEmployee.SELECTED_EMPLOYEE);
				txtNvID.setText(e.getEmployeeID());
				break;
			case SelectStock.SELECTED_STOCK_KEY:
				Stock s = (Stock) bundle
						.getSerializable(SelectStock.SELECTED_STOCK);
				txtKhoID.setText(s.getStockID());
				break;
			}
		}
	};
}



